package com.llynic.designdemo.proxymode.cglib;

import org.springframework.cglib.proxy.CallbackFilter;
import org.springframework.cglib.proxy.Enhancer;

import java.lang.reflect.Method;

public class App {

    public static void main(String[] args) {
        TargetObject tg = (TargetObject)new CglibProxy().newInstance(new TargetObject());
        tg.dating();
    }

    public void method1(){
        Enhancer enhancer = new Enhancer();
        TargetObject target = new TargetObject();
        //设置增强的回调
        enhancer.setCallback(new MyMethodInterceptor(target));

        //获得接口代理对象
        //对代理类的接口代理开始
        //enhancer.setInterfaces(new Class[]{});

        //对代理类的代理开始
        enhancer.setSuperclass(TargetObject.class);
        enhancer.setInterfaces(null);

        //如果有多个callback,那么需要通过callbackFilter来指定调用的是哪个callback
        enhancer.setCallbackFilter(new CallbackFilter() {
            @Override
            public int accept(Method method) {
                return 0;
            }
        });

        TargetObject tc = (TargetObject) enhancer.create();
        //tc.dating();
        //tc.playgame();
        tc.buy();
    }
}
